home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / misc / sci / ephem_src_4_28.lha / sex_dec.c < prev    next >
C/C++ Source or Header  |  1992-04-17  |  1KB  |  68 lines

  1. #include <math.h>
  2.  
  3. /* given hours (or degrees), hd, minutes, m, and seconds, s, 
  4.  * return decimal hours (or degrees), *d.
  5.  * in the case of hours (angles) < 0, only the first non-zero element should
  6.  *   be negative.
  7.  */
  8. sex_dec (hd, m, s, d)
  9. int hd, m, s;
  10. double *d;
  11. {
  12.     int sign = 1;
  13.  
  14.     if (hd < 0) {
  15.         sign = -1;
  16.         hd = -hd;
  17.     } else if (m < 0) {
  18.         sign = -1;
  19.         m = -m;
  20.     } else if (s < 0) {
  21.         sign = -1;
  22.         s = -s;
  23.     }
  24.  
  25.     *d = (((double)s/60.0 + (double)m)/60.0 + (double)hd) * sign;
  26. }
  27.  
  28. /* given decimal hours (or degrees), d.
  29.  * return nearest hours (or degrees), *hd, minutes, *m, and seconds, *s, 
  30.  * each always non-negative; *isneg is set to 1 if d is < 0, else to 0.
  31.  */
  32. dec_sex (d, hd, m, s, isneg)
  33. double d;
  34. int *hd, *m, *s, *isneg;
  35. {
  36.     double min;
  37.  
  38.     if (d < 0) {
  39.         *isneg = 1;
  40.         d = -d;
  41.     } else
  42.         *isneg = 0;
  43.  
  44.     *hd = (int)d;
  45.     min = (d - *hd)*60.;
  46.     *m = (int)min;
  47.     *s = (int)((min - *m)*60. + 0.5);
  48.  
  49.     if (*s == 60) {
  50.         if ((*m += 1) == 60) {
  51.         *hd += 1;
  52.         *m = 0;
  53.         }
  54.         *s = 0;
  55.     }
  56.     /* no  negative 0's */
  57.     if (*hd == 0 && *m == 0 && *s == 0)
  58.         *isneg = 0;
  59. }
  60.  
  61. /* insure 0 <= *v < r.
  62.  */
  63. range (v, r)
  64. double *v, r;
  65. {
  66.     *v -= r*floor(*v/r);
  67. }
  68.